<?php

namespace App\Http\Controllers\Home\order;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class orderController extends Controller
{
    //查询遍历
    public function index()
    {
    	$data = DB::table('orders')
    		->leftJoin('goods', 'orders.goods_id', '=', 'goods.id')
    		->leftJoin('cart', 'goods.id', '=', 'cart.goods_id')
    		->select('orders.post_status','orders.post_number', 'orders.addtime', 'orders.post_method', 'goods.goods_name', 'goods.goods_price','orders.goods_number')
            ->orderBy('orders.addtime', 'DESC')
    		->get();

        // dd(session('master'));
    	
    	return view('home.order.order', ['data' => $data]);
    }

    public function center(Request $request)
    {	 

        $uid = session('master')['id'];
        // dd($uid);
        foreach($request -> id as $v)
        {
            $data[]= DB::table('cart')
                -> select('cart.goods_number', 'goods.goods_price', 'goods.goods_name', 'goods.id', 'goods.logo') 
                -> leftJoin('goods', 'cart.goods_id', '=', 'goods.id')
                -> where('cart.id',$v)
                -> first();

            $address = DB::table('address')
                        -> where('uid',$uid )
                        -> get();   
        }
        // dd($address);
        // dd($data);
        return view('home.order.orderCenter', ['data' => $data, 'address' => $address]);     

    }

	//订单分类
	public function ajaxPay(Request $request)
    {
        $status = $request -> status;

        if($status == 5)
        {
	    	$data = DB::table('orders')
	    		->leftJoin('goods', 'orders.goods_id', '=', 'goods.id')
	    		->select('orders.post_status','orders.post_number', 'orders.addtime', 'orders.post_method','orders.id', 'goods.goods_name', 'goods.goods_price','orders.goods_number', 'orders.goods_id', 'goods.logo')
                ->orderBy('orders.addtime', 'DESC')
	    		->get();
    	
        }
        else
        {
        	$data = DB::select('SELECT orders.post_status, orders.post_number, orders.addtime, orders.post_method, orders.id, orders.goods_id,goods.goods_name, goods.goods_price,goods.logo, orders.goods_number FROM orders,goods WHERE orders.goods_id=goods.id AND orders.post_status=:status ORDER BY orders.addtime DESC', ['status' => $status]);
        }
    
        return $data;
        
    }

    //收银台
    public function checkstand(Request $request)
    {
        $data = array();

        $orderData = array();
// dd($request -> id);
        foreach($request -> id as $v)
        {
            // var_dump($v);
            $data = array();

            $data['addtime'] = time();

            $data['goods_number']  = DB::table('cart') -> where('goods_id', $v) -> first() -> goods_number;

            $data['post_number'] = time().mt_rand(10000,99999);

            $data['goods_id'] = $v;

            $data['post_status'] = 0;

            $data['post_method'] = 0;

            $price = DB::table('goods') -> where('id', $v) -> first() -> goods_price;

            $data['total_price'] =  $price * $data['goods_number'];

            $data['member_id'] = session('master')['id'];

            $id = DB::table('orders') -> insertGetId($data);

            if($id)
            {
                $orderData[] = DB::table('orders')
                    ->leftJoin('goods', 'goods.id', '=', 'orders.goods_id')
                    ->where('goods.id', $v)
                    ->select('orders.post_number', 'orders.addtime', 'goods.goods_name', 'goods.goods_price', 'orders.goods_number', 'orders.total_price', 'orders.id')
                    ->first();
            }

        }

        return view('home.order.checkstand', ['orderData' => $orderData]);
    }

    //订单详情
    public function orderdetail(Request $request)
    {
        $id = $request -> id;

        $data = DB::table('orders')
            ->leftJoin('address', 'address.uid', '=', 'orders.member_id')
            ->leftJoin('goods', 'goods.id', '=', 'orders.goods_id')
            ->where('orders.goods_id', $id)
            ->select('orders.post_number', 'orders.addtime', 'orders.total_price', 'orders.post_status', 'orders.goods_number','orders.total_price', 'address.*', 'goods.goods_name', 'goods.goods_price')
            ->get();

        // dd($data);

        return view('home.order.orderdetail', ['data' => $data]);
    }

    public function delete($id)
    {
        $data = DB::table('orders') -> where('id',$id) -> delete();

        return redirect('/home/order/order');
    }

//支付修改订单状态
    public function  postOrder(Request $request)
    {
      
        $id = array();
        
        foreach($request -> id as $v)
        {
            $data = DB::table('orders') -> where('id', $v) -> update(['post_status' => 1]);
        }
        return view('home.order.order', ['data' => $data]);
    }

    //订单里面支付
    public function orderpay($id)
    {   
        $data = DB::table('orders') -> where('id', $id) -> update(['post_status' => 1]);

        return back();
    }

    //确认收货
    public function sure($id)
    {
        $data = DB::table('orders') -> where('id', $id) -> update(['post_status' => 3]);

        return back();
    }

    //申请退款
    public function saleReturn($id)
    {
        $time = time();

        $time = DB::table('orders') -> where('id', $id) -> update(['donetime' => $time]);

        $data = DB::table('orders') -> where('id', $id) -> update(['post_status' => 6]);

        return back();
    }

    //退货列表
    public function indexReturn()
    {

        $data = DB::table('orders')
            ->leftJoin('goods', 'orders.goods_id', '=', 'goods.id')
            ->where('post_status', 6)
            ->select('orders.total_price', 'goods.goods_name', 'orders.donetime', 'orders.goods_number', 'orders.post_number', 'post_status')
            ->get();

        return view('home.order.saleReturn', ['data' => $data]);
    }
}
